package com.oreilly.ez.web;

import com.oreilly.ez.entity.SysUser;
import io.swagger.annotations.Api;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
@Api(value = "base", tags = "controller 基础")
public abstract class BaseCtrl<T> {


    @GetMapping
    public abstract Object find(T t, Pageable pageable);

    @GetMapping("/{id}")
    public abstract Object findOne(@PathVariable Integer id);

    @PostMapping
    public abstract Object save(@RequestBody @Valid T t);

    @PostMapping("/{id}")
    public abstract Object delete(@PathVariable Integer id);


    protected SysUser sessionUser(){

        //获取到当前线程绑定的请求对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//已经拿到session,就可以拿到session中保存的用户信息了。
        HttpSession session = request.getSession();
        SysUser userInfo = (SysUser) session.getAttribute("USER");
        System.out.println(userInfo);
        return userInfo;

    }

}
